#include "util/tc_common.h"
#include "util/tc_thread.h"
#include "jmem/jmem_hashmap.h"
#include "Test.h"

using namespace std;
using namespace taf;

typedef JceHashMap<Test::MapKey, Test::MapValue, ThreadLockPolicy, FileStorePolicy> HashMap;

HashMap g_hashmap;

class TestToDoFunctor : public HashMap::ToDoFunctor
{
    protected:
        virtual void erase(const TestToDoFunctor::DataRecord &data)
        {
            cout << "TestToDoFunctor::erase::" << data._key.s << ":" << data._value.s << ":" << data._dirty << ":" << data._iSyncTime << endl;
        }

        virtual void del(bool bExists, const TestToDoFunctor::DataRecord &data)
        {
            cout << "TestToDoFunctor::del::" << bExists << ":" << data._key.s << ":" << data._value.s << ":" << data._dirty << ":" << data.iSyncTime << endl;
        }

        virtual void sync(bool bExists, const TestToDoFunctor::DataRecord &data)
        {
            cout << "TestToDoFunctor::sync::" << bExists << ":" << data._key.s << ":" << data._value.s << ":" << data._dirty << ":" << data.iSyncTime << endl;
        }
        
        virtual void backup(bool bExists, const TestToDoFunctor::DataRecord &data)
        {
            cout << "TestToDoFunctor::backup::" << bExists << ":" << data._key.s << ":" << data._value.s << ":" << data._dirty << ":" << data.iSyncTime << endl;
        }
        virtual int get(bool bExists, const TestToDoFunctor::DataRecord &data)
        {
            ostringstream s;
            s << rand();
            data._value.s = s.str();
            cout << "TestToDoFunctor::get::" << data._key.s << ":" << data._value.s << endl;
            return TC_HashMap::RT_OK;
        }
};

TestToDoFunctor g_todo;

void initHashMap()
{
    size_t n = 2048;

    g_hashmap.initDataBlockSize(10, 20, 1);
    if (TC_File::isFileExist("hashmap.dat"))
    {
        n = TC_File::getFileSize("hashmap.dat", n);
    }
    g_hashmap.iniStore("hashmap.dat", n);
    g_hashmap.setAutoErase(true);
}

int main(int argc, char** argv)
{
    size_t n = 1024*1024*10;
    g_queue.initStore("test.jq", n);
    g_queue.initLock(1019);

    PushThread pt1;
    PushThread pt2;
    pt1.start();
    pt2.start();
    
    PopThread tt1;
    PopThread tt2;
    tt1.start();
    tt2.start();

    pt1.getThreadControl().join();
    
    return 0;
}
